﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.FMCG.Buz.Interfaces;
using HtmlAgilityPack;
using Microhard.FMCG.Buz.Models;

namespace Microhard.FMCG.Buz.Impls.KASystem.WUMart
{
    public class LoginParser : IHtmlParser<LoginResultModel>
    {
        public LoginResultModel ParseObject(string html)
        {
            LoginResultModel result = new LoginResultModel();
            if (string.IsNullOrEmpty(html))
                return result;
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(html);
            var span = doc.GetElementbyId("loginUser");
            if (span != null && span.InnerText == "321503")
            {
                result.IsSucceed = true;
                var hitDiv = doc.GetElementbyId("OrderDeliverHitDiv");
                if (hitDiv != null)
                {
                    //移除所有的class
                    var classTd = (from td in hitDiv.Descendants()
                                   where td.Attributes.Contains("class") || td.Attributes.Contains("style")
                                   select td).ToList();
                    classTd.ForEach(t =>
                    {
                        if (t.Attributes.Contains("class"))
                        {
                            t.Attributes.Remove("class");
                        }
                        else
                        {
                            t.Attributes.Remove("style");
                        }
                    });
                    result.Notice = hitDiv.InnerHtml;
                }
            }
            return result;
        }
    }
}
